
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-hazardExamineModuleConfig'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#hazardExamineModuleConfig-table-form'),
        $tenantSelect = $searchForm.find('select[name="tenantSelect"]'),
        $reviewTypeDiv = $view.find('#reviewTypeDiv'),
        tableIns;

    form.verify(validate);

    initFormValue();

    function initFormValue() {
        if (hazardExamineModuleConfig) {
            let reviewSwitch = hazardExamineModuleConfig.reviewSwitch;
            form.val("hazardExamineModuleConfig-form", {
                "moduleConfigId": hazardExamineModuleConfig.moduleConfigId,
                "tenantId": hazardExamineModuleConfig.tenantId,
                "reviewSwitch": reviewSwitch == 1 ? 'on' : '',
                "reviewType": hazardExamineModuleConfig.reviewType,
            });
            if (reviewSwitch == 1) {
                $reviewTypeDiv.show();
            }else {
                $reviewTypeDiv.hide();
            }

        }
    }



    function reloadPage() {
        var tenantId=$tenantSelect.val();
        febs.get(ctx + 'hazardExamineModuleConfig/search/' + tenantId, null, function (data) {
            var newHazardExamineModuleConfig = "";
            if(data.code==200&&data.data.hazardExamineModuleConfig){
                newHazardExamineModuleConfig = data.data.hazardExamineModuleConfig;
            }
            let reviewSwitch = newHazardExamineModuleConfig.reviewSwitch;
            form.val("hazardExamineModuleConfig-form", {
                "moduleConfigId": newHazardExamineModuleConfig.moduleConfigId,
                "tenantId": newHazardExamineModuleConfig.tenantId,
                "reviewSwitch": reviewSwitch == 1 ? 'on' : '',
                "reviewType": newHazardExamineModuleConfig.reviewType ? newHazardExamineModuleConfig.reviewType : '1',
            });
            if (reviewSwitch == 1) {
                $reviewTypeDiv.show();
            }else {
                $reviewTypeDiv.hide();
            }

        })
    }

    $query.on('click', function () {
        reloadPage();
    });

    // 监听审核开关
    form.on('switch(reviewSwitch)', function (data) {
        let status = data.elem.checked;
        if (status) {
            $reviewTypeDiv.show();
        }else {
            $reviewTypeDiv.hide();
        }
    })

    form.on('submit(hazardExamineModuleConfig-form-submit)', function (data) {
        if(data.field.tenantId==null || data.field.tenantId ==''){
            data.field.tenantId = $tenantSelect.val();
        }
        data.field.reviewSwitch = data.field.reviewSwitch == 'on' ? 1 : 0
        febs.modal.confirm('保存提示', '是否保存配置信息？', function () {
            if (data.field.moduleConfigId && data.field.moduleConfigId.length > 0) {
                febs.postArray(ctx + 'hazardExamineModuleConfig/update', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('保存成功');
                });
            } else {
                febs.postArray(ctx + 'hazardExamineModuleConfig', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('保存成功');
                    reloadPage();
                });
            }
        });

        return false;
    });


    form.render();


});